// pages/staff_mine/index/index.js

const app = getApp();
import * as echarts from '../../../utils/ec-canvas/echarts';
let chart;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        chart_ec: {},
        chart_data: {
            complete: 0,
            immature: 0
        },
        chart_ready: false,
        app_name: '',
        manager_phone: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        // 验证登录
        app.verifyLogin();

        // 获取数据
        app.execute(function() {
            let page = app.getPage();
            page.getOrderInfo(page);
            page.getSetting(page);
        });

    },

    /**
     * 自定义函数--获取数据
     */
    getOrderInfo: function(page) {

        if (true !== page.data.chart_ready){
            setTimeout(function(){
                page.getOrderInfo(page);
            }, 200);
            return;
        };

        wx.showLoading({
            title: '获取信息...',
        });
        wx.request({
            url: app.data.service_url + 'staff_recycle_order/statistics_all',
            header: {
                'Client-Token': app.data.client_token,
            },
            method: 'GET',
            success: function(response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (9 === response.data.errno) {
                        wx.redirectTo({
                            url: app.data.staff_uncertified_jump + '?message=' + response.data.message,
                        });
                    } else if (0 === response.data.errno) {
                        page.setData({
                            chart_data: {
                                complete: response.data.data.complete_num,
                                immature: response.data.data.immature_num,
                            }
                        });
                        page.changeEchart();
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    },

    /**
     * 自定义函数--获取配置项
    */
    getSetting: function(page) {

        let data = {
            field: ['app_name', 'manager_phone'],
        };
        wx.request({
            url: app.data.service_url + 'staff_index/get_setting',
            header: {
                'Client-Token': app.data.client_token,
            },
            method: 'POST',
            data: data,
            success: function (response) {
                if (200 === response.statusCode) {
                    if (9 === response.data.errno) {
                        wx.redirectTo({
                            url: app.data.staff_uncertified_jump + '?message=' + response.data.message,
                        });
                    } else if (0 === response.data.errno) {
                        page.setData(response.data.data);
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function () {
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    },

    /**
     * 自定义函数--初始化图表
     */
    initEchart: function(e) {

        chart = echarts.init(e.detail.canvas, null, {
            width: e.detail.width,
            height: e.detail.height
        });
        e.detail.canvas.setChart(chart);

        this.changeEchart();

    },

    /**
     * 自定义函数--更改图表数据
     */
    changeEchart: function () {
        let option = {
            backgroundColor: "#ffffff",
            color: ["#37A2DA", "#FFDB5C"],
            series: [{
                label: {
                    normal: {
                        fontSize: 14
                    }
                },
                type: 'pie',
                center: ['50%', '50%'],
                radius: [0, '60%'],
                data: [{
                    value: this.data.chart_data.immature,
                    name: '当前未完成'
                }, {
                    value: this.data.chart_data.complete,
                    name: '本月已完成'
                }],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 2, 2, 0.3)'
                    }
                }
            }]
        };
        chart.setOption(option);
        this.data.chart_ready = true;
    }

});